#ifndef __COMMAND_H__
#define __COMMAND_H__
#include "mstring.h"
#include "terminal.h"
#include "shstyle.h"
#include "mxsetup.h"

/* help support, if use this macro, must place it on the first line of function */
#ifdef MXSETUP_COMMAND_SUPPORT_HELP
const static mx_string_pt __rus_help = "--help";
const static mx_string_pt __rus_h = "-h";
#define mx_command_help(note) { \
    if (argc == 2 &&    \
        (mx_strcmp(argv[1], __rus_help) == 0 || mx_strcmp(argv[1], __rus_h) == 0)) { \
        mx_shstyle_change(MX_SHSTYLE_MODE_WARN);    \
        console_puts(note ENDL); \
        return MX_TERMINAL_EXECRET_OK;   \
    }   \
}
#else
#define mx_command_help(note) { /* do nothing */ }
#endif  /* MXSETUP_COMMAND_SUPPORT_HELP */

/* check the usage error */
#ifdef MXSETUP_COMMAND_SUPPORT_CHECKNOTE
const static mx_string_pt __rus_argcerr = "error: argument count %s";
#define mx_command_ckerror(fcond) {   \
    if (fcond) { \
        mx_shstyle_change(MX_SHSTYLE_MODE_ERROR);   \
        consoleline(__rus_argcerr, "("#fcond")");    \
        return MX_TERMINAL_EXECRET_ERR_USAGE;   \
    }   \
}
#else
#define mx_command_ckerror(fcond) {   \
    if (fcond) { \
        return MX_TERMINAL_EXECRET_ERR_USAGE;   \
    }   \
}
#endif  /* MXSETUP_COMMAND_SUPPORT_CHECKNOTE */

/* print warning message */
#ifdef MXSETUP_COMMAND_SUPPORT_WARN
const static mx_string_pt __rus_warn = " warn: ";
#define mx_command_pwarn(msg) { \
    mx_shstyle_change(MX_SHSTYLE_MODE_WARN);    \
    console_puts(__rus_warn);   \
    console_puts(msg ENDL);  \
}
#else
#define mx_command_pwarn(msg) { /* do nothing */ }
#endif  /* MXSETUP_COMMAND_SUPPORT_WARN */

/* print error message */
#ifdef MXSETUP_COMMAND_SUPPORT_ERROR
const static mx_string_pt __rus_erro = "error: ";
#define mx_command_perror(msg) { \
    mx_shstyle_change(MX_SHSTYLE_MODE_ERROR);   \
    console_puts(__rus_erro);   \
    console_puts(msg ENDL);  \
}
#else
#define mx_command_perror(msg) { /* do nothing */ }
#endif  /* MXSETUP_COMMAND_SUPPORT_ERROR */

/* recover the all of arguments value to a string */
#define mx_command_argvs2str() ({    \
    mx_string_pt ret = mx_null;  \
    if (argc > 1) { \
        for (mx_string_pt s = &argv[1][0]; s < &argv[argc - 1][0]; s++) {   \
            if (*s == '\0') \
                *s = ' ';   \
        }   \
        ret = &argv[1][0];    \
    }   \
    ret;    \
})

#endif
